package com.example.javafx.文本域;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 * 覆盖replaceText和replaceSelection以创建自定义的TextField
 */
public class Main_4 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        Group root = new Group();
        Scene scene = new Scene(root,300,250, Color.WHITE);

        TextField field = new TextField(){
            @Override
            public void replaceText(int start, int end, String text) {

                if(!text.matches("[a-z]")){
                    super.replaceText(start,end,text);
                }
            }

            @Override
            public void replaceSelection(String replacement) {
                if(!replacement.matches("[a-z]")){
                    super.replaceSelection(replacement);
                }

            }
        };

        root.getChildren().add(field);
        primaryStage.setScene(scene);
        primaryStage.show();

    }
}
